<?php
session_start();


include_once '/class_master/db.class.php';
    include('includes/fonction.php');
   if (isset($_POST['hidSalle']) && $_POST['hidSalle'] == '1'){
    	foreach($_POST['selectSalle'] as $chkbx)
    	{
    		$selectSalle = $chkbx;
    		if(!empty($selectSalle))
    		{
    			
	            $db = new db();
	            $SqlContrainte = "id_salle = $selectSalle";
	            $req = $db->select('id_machine','machine', $SqlContrainte);
	            while($row = $req->fetch())
	            {
		            $Sql = " id_machine = $row[0] ";
		            $db->delete('incident', $Sql);
		            $db->delete('machine', $Sql);
	            }
	            $db->delete('salle', $SqlContrainte);
	            header("Location: gestion_salle.php");
    		}
    		
    	}
    
    }else if (isset($_POST['hidMachine']) && $_POST['hidMachine'] == '2'){
        
         
         
         foreach($_POST['id_machine'] as $chkbx)
         {
         	$id_machine = $chkbx;
         	if(!empty($id_machine))
         	{
         		$db = new db();
         		$Sql = " id_machine = $id_machine ";
         		$db->delete('incident', $Sql);
         		$db->delete('machine', $Sql);
		        

		        header("Location: gestion_salle.php");
             
         	}
         }
    } else if (isset($_POST['hidIncident']) && $_POST['hidIncident'] == '3'){
         
         
         
         foreach($_POST['id_incident'] as $chkbx)
         {
         	$id_incident = $chkbx;
         	
         	if(!empty($id_incident))
         	{
         		$db = new db();
		        $Sql = " id_incident = $id_incident ";
		        $db->delete('incident', $Sql);
		        
		        $id_machine = $_POST['id_machine'];
		        $SqlContrainte = "id_machine = $id_machine";
		        
         	 	$req = $db->select('id_incident','incident', $SqlContrainte);
		        $return = new parcourir();
		        $res = $return->get_unique($req);
		        if(empty($res)){
		        	$SqlValues =  $id_machine;
		        	$db->update_status_machine_0('machine', $SqlValues);
		        }
		        
		        
		        $SqlContrainte = " id_machine= $id_machine";
		        $req = $db->select('id_salle','machine', $SqlContrainte);
		        $return = new parcourir();
		        $res = $return->get_unique($req);
		        
		        $SqlContrainte = "status=1 and id_salle = $res";
		        $req1 = $db->select('id_machine','machine', $SqlContrainte);
		        $return = new parcourir();
		        $res0 = $return->get_unique($req1);
		        if(empty($res0)){
		        	$SqlValues =  $res;
		        	$db->update_status_salle_0('salle', $SqlValues);
		        }
		        header("Location: gestion_salle.php");
             
         	}
         }
    } 

  
    
 
    
    if(isset($_GET['id_salle'])){
    	$id_salle = $_GET['id_salle'];
    }elseif ($_POST['id_salle']){
    	$id_salle = $_POST['id_salle'];
    	header("Location: gestion_machine.php?id_salle=$id_salle");
    }
    
  
    
    
    include('includes/header.php');
?>
    
    <div id="contain">
        <?php include("includes/monitorEntete.php"); ?>
        <div id="corp">
            <div class="change scroll" <?php echo $hidSalle;?>>
            	<div class="title">Suppression de Salle</div>
            
                <form action="supp.php" method="post">
                     <input type="hidden" name="hidSalle" id="hidSalle" value="1" />
                     <table class="table table-striped">
                        <?php
                        $db = new db();
                        $req = $db->select_salle('id_salle, nom_salle');
                        while ($row = $req->fetch())
                        { 
                        echo ' 
                        <tr>
    		      	 		<td>'.$row[1].'</td>
                            	<td class="alignRight">
                                <input type="checkbox" name="selectSalle[]" value="'.$row[0].'">
         		          	</td>
                        </tr>';
                        }
                        ?>
                     </table>
                      <button class="btn" type="submit">Valider</button>
                </form>
            </div>
            
            <div class="change scroll supp" <?php echo $hidMachine;?>>
            	<div class="title">Suppression de Machine</div>
            
              	<form action="supp.php"  method="post">
                    <input type="hidden" name="hidMachine" id="hidMachine" value="2" />
                  
                    <table class="table table-striped" >
                        
                        <?php 
                        $id_salle = $_GET['id_salle'];
                        echo '<input type="hidden" name="id_salle" id="id_salle" value="'.$id_salle.'" />';
                         
                        $db = new db();
                        $req = $db->select('id_machine, nom_machine, id_salle','machine','id_salle = '.$id_salle);
                        while ($row = $req->fetch())
                        {
                        	$id_machine = $row[0];
                        	$nom_machine = $row[1]; 
                        	$id_salle = $row[2];
                           echo 
                           '<tr>
             					
                           		<td>'.$nom_machine.'</td>
                            	<td class="alignRight">
                                <input type="checkbox" name="id_machine[]"  value="'.$id_machine.'">
    						 	</td>
                        	</tr>';
        				}
                        ?>
                                     
                     </table>
                    <button class="btn" type="submit">Valider</button>
                </form> 
            </div>  
            
            
                <div class="change scroll supp" <?php echo $hidIncident;?>>
            	<div class="title">Suppression d'Incident</div>
            
              	<form action="supp.php"  method="post">
                    <input type="hidden" name="hidIncident" id="hidIncident" value="3" />
                  
                    <table class="table table-striped" >
                        
                        <?php 
                        $id_salle = $_GET['id_salle'];
                         echo '<input type="hidden" name="id_salle" id="id_salle" value="'.$id_salle.'" />';
                        
                        $id_machine = $_GET['id_machine'];
                        echo '<input type="hidden" name="id_machine" id="id_machine" value="'.$id_machine.'" />';
                        
                        $db = new db();
                        $return = new parcourir();
                        
                        
                        
                        $res = $req = $db->select('nom_machine','machine','id_machine ='.$id_machine);
                        $result = $return->get_unique($res);
                        
                        $req = $db->select('*','incident','id_machine ='.$id_machine);
                        while ($row = $req->fetch())
                        {
                        	
                        	
                        	$id_incident = $row[0];
                        	$deb_i =$row[1];
                        	$com_i =$row[5];
                        	$nom_m = $deb_i .' : '.$row[3].' : '.$com_i.' : ';
                        	$id_m = $row[6];
                        	
                        	
                        	$nom_m = $nom_m.$result;
                        	
                           echo 
                           '<tr>
             					
                           		<td>'.$nom_m.'</td>
                            	<td class="alignRight">
                                <input type="checkbox" name="id_incident[]"  value="'.$id_incident.'">
    						 	</td>
                        	</tr>';
        				}
                        ?>
                                     
                     </table>
                    <button class="btn" type="submit">Valider</button>
                </form> 
            </div>           
        </div>       
        <div id="main">
        <?php include('includes/main.php'); ?>
        </div>
        <?php include('includes/etatEnDirect.php'); ?>
        
    </div>
<?php include('includes/footer.php'); ?>
</div>
</div>